"""
@Author  : 康帅
@Time    : 2021/3/17 9:41
@Function: 职责模式(责任链模式)：请假案列
"""
from abc import ABCMeta, abstractmethod


class Person:
    """ 请假人 """

    def __init__(self, name, day_off, reason):
        self.__name = name
        self.__day_off = day_off
        self.__reason = reason
        self.__leader = None

    def get_name(self):
        return self.__name

    def get_day_off(self):
        return self.__day_off

    def get_reason(self):
        return self.__reason

    def set_leader(self, leader):
        self.__leader = leader

    def request(self):
        print("%s申请请假%d天。请假事由：%s" % (self.__name, self.__day_off, self.__reason))
        if self.__leader is not None:
            self.__leader.handler_request(self)


class Manager(metaclass=ABCMeta):
    """ 公司管理人员 """

    def __init__(self, name, title):
        self.__name = name
        self.__title = title
        self._next_handler = None

    def get_name(self):
        return self.__name

    def get_title(self):
        return self.__title

    def set_next_handler(self, next_handler):
        self._next_handler = next_handler

    @abstractmethod
    def handler_request(self, person):
        pass


class Supervisor(Manager):
    """ 主管 """

    def __init__(self, name, title):
        super().__init__(name, title)

    def handler_request(self, person):
        if person.get_day_off() <= 2:
            print("同意%s请假，签字人：%s(%s)" % (person.get_name(), self.get_name(), self.get_title()))
        if self._next_handler is not None:
            self._next_handler.handler_request(person)


class DepartmentManager(Manager):
    """ 部门总监 """

    def __init__(self, name, title):
        super().__init__(name, title)

    def handler_request(self, person):
        if person.get_day_off() > 2 and person.get_day_off() <= 5:
            print("同意%s请假，签字人：%s(%s)" % (person.get_name(), self.get_name(), self.get_title()))
        if self._next_handler is not None:
            self._next_handler.handler_request(person)


class Ceo(Manager):
    """ CEO """

    def __init__(self, name, title):
        super().__init__(name, title)

    def handler_request(self, person):
        if person.get_day_off() > 5 and person.get_day_off() <= 22:
            print("同意%s请假，签字人：%s(%s)" % (person.get_name(), self.get_name(), self.get_title()))
        if self._next_handler is not None:
            self._next_handler.handler_request(person)


class Administrator(Manager):
    """ 行政人员 """

    def __init__(self, name, title):
        super().__init__(name, title)

    def handler_request(self, person):
        print("%s的请假申请已审核，情况属实！已备案处理。处理人：%s(%s)\n" % (person.get_name(), self.get_name(), self.get_title()))


if __name__ == '__main__':
    direct_leader = Supervisor("Jack", "客户端研发部经理")
    department_leader = DepartmentManager("Kim", "技术研发部总监")
    ceo = Ceo("Helen", "智标数据ceo")
    administrator = Administrator("Nina", "行政中心总监")
    direct_leader.set_next_handler(department_leader)
    department_leader.set_next_handler(ceo)
    ceo.set_next_handler(administrator)

    sunny = Person("Sunny", 1, "参加MDCC大会")
    sunny.set_leader(direct_leader)
    sunny.request()

    tony = Person("Tony", 5, "家里有事")
    tony.set_leader(direct_leader)
    tony.request()

    pony = Person("Pony", 15, "学术研究")
    pony.set_leader(direct_leader)
    pony.request()
